//
//  ColorMacros.swift
//  Swift4.0_Learn
//
//  Created by lantaiyuan on 2017/10/24.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//

import Foundation
import UIKit

// MARK: - 获取颜色的方法
extension UIColor{
    
    /// 获取颜色，通过16进制色值字符串，e.g. #ff0000， ff0000
    ///
    /// - Parameters:
    ///   - hex: 16进制字符串
    ///   - alpha: 透明度，默认为1，不透明
    /// - Returns: RGB
    static func withHex(hexstring hex:String,alpha:CGFloat = 1) ->UIColor{
        // 去除空格等
        var cString:String = hex.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines).uppercased()
        // 去除#
        if cString.hasPrefix("#")  {
            cString = (cString as String).substring(from: cString.index)
        }
    }
}

extension UIColor {
    /**
     
     - parameter hexString  :
     - parameter alpha      :
     - returns:
     */
    static func withHex(hexString hex: String, alpha:CGFloat = 1) -> UIColor {
        // 去除空格等
        var cString: String = hex.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines).uppercased()
        // 去除#
        if (cString.hasPrefix("#")) {
            cString = (cString as NSString).substring(from: 1)
        }
        // 必须为6位
        if (cString.characters.count != 6) {
            return UIColor.gray
        }
        // 红色的色值
        let rString = (cString as NSString).substring(to: 2)
        let gString = ((cString as NSString).substring(from: 2) as NSString).substring(to: 2)
        let bString = ((cString as NSString).substring(from: 4) as NSString).substring(to: 2)
        // 字符串转换
        var r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0
        Scanner(string: rString).scanHexInt32(&r)
        Scanner(string: gString).scanHexInt32(&g)
        Scanner(string: bString).scanHexInt32(&b)
        
        return UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: alpha)
}

///**获取RGB颜色*/
//public func RGBA(r:CGFloat,g:CGFloat,b:CGFloat,a:CGFloat) -> UIColor {
//    return UIColor(red: r / 255.0, green: g / 255.0, blue: b / 255.0, alpha: a)
//}
//public func RGB(r:CGFloat,g:CGFloat,b:CGFloat) -> UIColor {
//    return RGBA(r: r, g: g, b: b, a: 1.0)
//}
//
//
///*默认颜色*/
//func RGBColor(r :CGFloat ,g:CGFloat,b:CGFloat) ->UIColor{
//    return UIColor.init(red: r / 255, green: g / 255, blue: b / 255, alpha: 1)
//}
//func RGBCOLOR_HEX(h:Int) ->UIColor {
//    return RGBColor(r: CGFloat(((h)>>16) & 0xFF), g:   CGFloat(((h)>>8) & 0xFF), b:  CGFloat((h) & 0xFF))
//}
//
//// 导航栏颜色
//public let NAV_BAR_COLOR = RGBCOLOR_HEX(3b3a3f) UIColor.qmui_color(withHexString: "#3b3a3f")
//
//// 导航栏标题颜色
//public let NAV_TITLE_COLOR = UIColor.white
//
//// 标签栏颜色（高亮）
//public let TABBAR_COLOR_HIGHLIGHTED = UIColor.qmui_color(withHexString: "#00a73c")
//
//// 标题颜色
//public let TITLE_COLOR = UIColor.qmui_color(withHexString: "#333333")
//
//// 副标题颜色
//public let SUB_TITLE_COLOR = UIColor.qmui_color(withHexString: "#666666")
//// 灰色
//public let GRAY_COLOR = UIColor.qmui_color(withHexString: "#999999")
//
//// 背景色
//public let CONTENT_BG_COLOR  = UIColor.qmui_color(withHexString: "#f5f5f5")
//
//// 分隔线颜色
//public let SEPARATE_LINE_COLOR = UIColor.qmui_color(withHexString: "#dddddd")
//
//// 边框线颜色
//public let BORDER_COLOR = UIColor.qmui_color(withHexString: "#faaf19")
//
//// 边框线颜色(灰色)
//public let BORDER_COLOR_GRAY = UIColor.qmui_color(withHexString: "#dddddd")
//// 占位文字颜色
//public let PLACEHOLDER_COLOR = UIColor.qmui_color(withHexString: "#bbbbbb")
//// 边框颜色
//public let WORK_LINE_COLOR = UIColor.qmui_color(withHexString: "#bbbbbb")
//// 错误提示文字颜色
//public let ERROR_COLOR = UIColor.qmui_color(withHexString: "#fa4535")
//
//// 成功文字颜色
//public let SUCCESS_COLOR = UIColor.qmui_color(withHexString: "#5298ff")
//
//// 支付颜色
//public let PAY_COLOR = UIColor.qmui_color(withHexString: "#faaf19")
//
//// 按住时的颜色
//public let PRESS_COLOR = UIColor.qmui_color(withHexString: "#1d7f38")
//
//// 绿色
//public let GREEN_COLOR = UIColor.qmui_color(withHexString: "#2ab650")
//// 红色
//public let RED_COLOR = UIColor.qmui_color(withHexString: "#fa4535")
//// 橘色
//public let ORANGE_COLOR = UIColor.qmui_color(withHexString: "#fa483a")
//
//// 白色
//public let WHITE_COLOR = UIColor.qmui_color(withHexString: "#ffffff")
////深灰
//public let DEEP_GRAY_COLOR = UIColor.qmui_color(withHexString:"#7a7a7a")
//// 轮播条颜色
//public let pageDot_Color = UIColor.qmui_color(withHexString:"#229240")
//
//// 地图线条颜色
//public let MAP_LINE_COLOR = UIColor.qmui_color(withHexString:"#5298ff")
//
////日历背景色
//public let CALENDAR_BG_COLOR = UIColor.qmui_color(withHexString:"#dddddd")
////日历文字色
//public let CALENDAR_SUBTITLE_COLOR = UIColor.qmui_color(withHexString:"#AAAAAA")
////评价页面按钮
//public let Emergency_COLOR = UIColor.qmui_color(withHexString:"#bbbbbb")
////橘色
//public let ORANGEe_COLOR = UIColor.qmui_color(withHexString:"#fa9338")
////橘红色
//public let RED_ORANGE_COLOR = UIColor.qmui_color(withHexString:"#fb6a67")
////紫色
//public let PUPO_COLOR = UIColor.qmui_color(withHexString:"#d989f7")
////蓝色
//public let BLUE_COLOR = UIColor.qmui_color(withHexString:"#5da7fa")
////深蓝色
//public let DipBlue_COLOR = UIColor.qmui_color(withHexString:"#00ccff")
////紫色
//public let purple_color = UIColor.qmui_color(withHexString:"#5cccfa")
////发现定制公交颜色
//public let discover_purple_color = UIColor.qmui_color(withHexString:"#ff9a0a")
////拼车订单待支付
//public let discover_red_color = UIColor.qmui_color(withHexString:"#fb5042")
//public let LoginTextfieldColor = UIColor.qmui_color(withHexString:"#7ecc8f")
//public let setPayWordColor = UIColor.qmui_color(withHexString:"#b4b4b4")
    /// 获取颜色的方法
    extension UIColor {
        /**
         获取颜色，通过16进制色值字符串，e.g. #ff0000， ff0000
         - parameter hexString  : 16进制字符串
         - parameter alpha      : 透明度，默认为1，不透明
         - returns: RGB
         */
        static func withHex(hexString hex: String, alpha:CGFloat = 1) -> UIColor {
            // 去除空格等
            var cString: String = hex.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines).uppercased()
            // 去除#
            if (cString.hasPrefix("#")) {
                cString = (cString as NSString).substring(from: 1)
            }
            // 必须为6位
            if (cString.characters.count != 6) {
                return UIColor.gray
            }
            // 红色的色值
            let rString = (cString as NSString).substring(to: 2)
            let gString = ((cString as NSString).substring(from: 2) as NSString).substring(to: 2)
            let bString = ((cString as NSString).substring(from: 4) as NSString).substring(to: 2)
            // 字符串转换
            var r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0
            Scanner(string: rString).scanHexInt32(&r)
            Scanner(string: gString).scanHexInt32(&g)
            Scanner(string: bString).scanHexInt32(&b)
            
            return UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: alpha)
        }
        
        /**
         获取颜色，通过16进制数值
         - parameter hexInt : 16进制数值
         - parameter alpha  : 透明度
         - returns : 颜色
         */
        static func withHex(hexInt hex:Int32, alpha:CGFloat = 1) -> UIColor {
            let r = CGFloat((hex & 0xff0000) >> 16) / 255
            let g = CGFloat((hex & 0xff00) >> 8) / 255
            let b = CGFloat(hex & 0xff) / 255
            return UIColor(red: r, green: g, blue: b, alpha: alpha)
        }
        
        /**
         获取颜色，通过rgb
         - parameter red    : 红色
         - parameter green  : 绿色
         - parameter blue   : 蓝色
         - returns : 颜色
         */
        static func withRGB(_ red:CGFloat, _ green:CGFloat, _ blue:CGFloat) -> UIColor {
            return UIColor.withRGBA(red, green, blue, 1)
        }
        
        /**
         获取颜色，通过rgb
         - parameter red    : 红色
         - parameter green  : 绿色
         - parameter blue   : 蓝色
         - parameter alpha  : 透明度
         - returns : 颜色
         */
        static func withRGBA(_ red:CGFloat, _ green:CGFloat, _ blue:CGFloat, _ alpha:CGFloat) -> UIColor {
            return UIColor(red: red / 255, green: green / 255, blue: blue / 255, alpha: alpha)
        }
}
